package com.vue.vuedemo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vue.vuedemo.annotation.CheckToken;
import com.vue.vuedemo.map.CommentMapper;
import com.vue.vuedemo.pojo.Comment;
import com.vue.vuedemo.pojo.News;
import com.vue.vuedemo.pojo.User;
import com.vue.vuedemo.util.ListUtils;
import com.vue.vuedemo.viewpojo.ResList;
import com.vue.vuedemo.viewpojo.ResObject;
import com.vue.vuedemo.vo.CommentVo;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class CommentController {

    @Autowired
    CommentMapper commentMapper;
    
    @CheckToken
    @GetMapping("/getcomments/{id}")
    public ResList<CommentVo>  getCommentByUid(HttpServletResponse response,int pageindex,@PathVariable("id") int id){
//    public PageInfo<Comment>  getCommentByUid(HttpServletResponse response,int pageindex,@PathVariable("id") int id,String flag){

        List<Comment> commentByid=null;
        // 使用分页
        PageHelper.startPage(pageindex,10,"id desc");
        response.setHeader("Access-Control-Allow-Origin", "*");

//        if(flag.equals("news")){
//            commentByid= commentMapper.getCommentByNid(id);
//        }else if(flag.equals("photo")){
//            commentByid = commentMapper.getCommentByPid(id);
//        }else{
//            commentByid = commentMapper.getCommentByGid(id);
//        }
        
        commentByid= commentMapper.getCommentByid(id);
        PageInfo<Comment> page = new PageInfo<>(commentByid);
        
        ListUtils<CommentVo> utils = BeanUtils.instantiate(ListUtils.class);
        List<CommentVo> userInfoListTarget = new ArrayList();
        utils.copyList(page.getList(), userInfoListTarget, CommentVo.class);
        
        ResList<CommentVo> resList = new ResList();
        resList.setStatus(0);
        resList.setMessage(userInfoListTarget);
        
        return resList;
    }

    @CheckToken
    @PostMapping("/postcomment/{id}")
    public ResObject<String> postComment(HttpServletResponse response, @PathVariable("id") int id, HttpServletRequest request){
//    public void postComment(HttpServletResponse response, @PathVariable("id") int id, HttpServletRequest request,String flag){

        response.setHeader("Access-Control-Allow-Origin", "*");
        String content = request.getParameter("content");
        Comment comment = new Comment();
        comment.setUser_name("匿名用户");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        comment.setAdd_time(dateFormat.format(new Date()));
        comment.setContent(content);
        comment.setId(id);
        
//        if(flag.equals("news")){
//            comment.setNid(id);
//            comment.setPid(null);
//            comment.setGid(null);
//        }else if(flag.equals("photo")){
//            comment.setNid(null);
//            comment.setPid(id);
//            comment.setGid(null);
//        }else{
//            comment.setNid(null);
//            comment.setPid(null);
//            comment.setGid(id);
//        }


        commentMapper.postcomment(comment);
        
        ResObject<String> res = new ResObject();
        res.setStatus(0);
        res.setMessage("评论提交成功");
        
        return res;


    }
}
